package com.sff.utils {
	

	public class ArrayCollection {
	
		//firt in first out
		public static const FIFO 	: Number = 0;
		
		//last in first out
		public static const LIFO 	: Number = 1;
		
		protected var _items 		: Array;
		protected var _type		 	: Number = 0;
				
		public function ArrayCollection(args:Array) {			
			init(args);
		}
		
		protected function init( args : Array ) : void {
			_items = new Array();
			
			var initArray : Array = (args && args.length == 1 && args[0] is Array) ? args[0] : args;
			
			for each( var item : * in initArray ) {
				if( item ) {
					addItem(item);
				}
			}
		}	
	
		public function get type () : Number { 
			return _type ; 
		}
		
		public function set type ( val : Number ) : void { 
			_type = (val == 1) ? 1 : 0; 
		}
		
		public function next() : Object {
			if( _type == 1 ) return removeItemAt( length-1 );
			return removeItemAt( 0 );
		}
	
		public function hasNext() : Boolean {
			return ( length > 0 );
		}
		
		public function array() : Array {
			return new Array().concat( _items );	
		}
		
		public function get length() : uint {
			return _items.length;	
		}
		
		public function sortOn( names : *, options : uint = 0) : ArrayCollection {
			var ret : Array = _items.sortOn( names, options );
			if( ret ) return new ArrayCollection( ret );
			return null;
		}
		
		public function addItem( item : Object) : int {
			if( ! item ) {
				return -1;
			}
			
			if( indexOf(item) > -1 ) {
				return -1;
			}
			
			return _items.push( item ) - 1;
		}		
		
		
		public function addItemAt( item : Object, index : uint ) : void {
			if( ! item ) return;
			if( indexOf(item) > -1 ) return;
			
			if( index >= _items.length  ) addItem( item );
			
			else {
				for( var i : Number = _items.length; i > index ; i-- ) {
					_items[i] = _items[i - 1];
				}
				_items[ index ] = item;
			} 
		}		
		
		
		public function removeItem ( item : Object) : int {
			var index : int = indexOf( item );
			if( index > -1 ) {
				removeItemAt( index );
			}
			return index;
		}
		
		
		public function removeItemAt ( index : uint ) : Object {
			if( index < _items.length ) {
				var item : * = _items[index];
				for( var i : Number = index; i < _items.length - 1 ; i++ ) _items[i] = _items[i+1];
				_items.pop( );
				return item;
			}
			return null;
		}
		
		
		public function setItemIndex ( item : Object, index : uint ) : void {
			if( indexOf( item ) == index ) return;
			if( removeItem( item ) > -1 )
				addItemAt( item, index );
		}
		
		
		public function swapItems ( item1 : Object, item2 : Object) : void {
			var i1 : int = indexOf( item1 );
			var i2 : int = indexOf( item2 );
			
			if( i1 != -1 && i2 != -1 ) {
				_items[i1] = item2;
				_items[i2] = item1;
			}
		}
		
		
		public function swapItemsAt ( index1 : uint, index2 : uint ) : void {
			var item2:Object = _items[index2];
			_items[index2] = _items[index1];
			_items[index1] = item2;			
		}
		
		
		public function indexOf ( item : Object, fromIndex : int = 0 ) : int {
			return _items.indexOf( item, fromIndex );
		}
		
		public function getItemAt ( index : uint ) : Object {
			return _items[index];
		}
		
		public function replaceItem( oldItem : Object, newItem : Object) : Boolean {
			if( oldItem === newItem || newItem == null) return false;
			var index : int = _items.indexOf( oldItem );
			if( index > - 1 ) _items[ index ] = newItem;
			return ( index > - 1  );
		}
		
		public function toString() : String {
			var str : String ="";
			for( var i : String in _items ) {
				str += _items[i] + "\n";
			}
			return str;
		}			
	}
}
